package LUDOSimulator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Helper
{
	public static boolean isUnique(int[] toCheck, int a)
	{
		for (int i = 0; i < toCheck.length; i++)
		{
			if (toCheck[i] == a)
				return false;
		}
		return true;
	}

	public static List<PlayerResult> findBestPlayers(List<PlayerResult> result)
	{
		int endIndex = result.size() / 5;
		if (!(endIndex % 2 == 0))
			endIndex++;
		return result.subList(0, endIndex);
	}

	public static List<PlayerResult> sortPlayers(List<PlayerResult> result)
	{
		ArrayList<PlayerResult> res = new ArrayList<PlayerResult>();
		
		for (PlayerResult pr : result)
		{
			if (res.size() == 0)
				res.add(0, pr);
			else
			{
				for (int i = 0; i <= res.size(); i++)
				{
					if (res.get(i).getResult() + res.get(i).getWins() * 3 < pr.getResult() + pr.getWins() * 3)
					{
						res.add(i, pr);
						break;
					}
					if (i == res.size() - 1)
					{
						res.add(i+1, pr);
						break;
					}
				}
			}
		}
		return res;
	}

	public static List<LUDOPlayer> createPopulation(List<PlayerResult> parentSelection)
	{
		List<LUDOPlayer> toReturn = new ArrayList<LUDOPlayer>();
		for (int i = 0; i < parentSelection.size(); i = i + 2)
		{
			toReturn.add(parentSelection.get(i).getPlayer());
			toReturn.add(parentSelection.get(i+1).getPlayer());
			int j = 0;
//			if (i == 0) j = 20;
//			if (i == 2) j = 10;
//			if (i == 4) j = 5;
//			if (i == 6) j = 3;
//			if (i == 8) j = 2;
			if (i == 0) j = 36;
			if (i == 2) j = 18;
			if (i == 4) j = 9;
			if (i == 6) j = 5;
			if (i == 8) j = 3;
			if (i == 10) j = 2;
			if (i == 12) j = 2;
			if (i == 14) j = 2;
			if (i == 16) j = 2;
			if (i == 18) j = 1;
			
			for (int x = 0; x < j; x++)
			{
				DbjLUDOPlayer p = (DbjLUDOPlayer) performTwoPointCrossover(parentSelection.get(i).getPlayer(), parentSelection.get(i+1).getPlayer());
				p.setWeights(mutateWeights(p.getWeights()));
				toReturn.add(p);
			}
		}
		Collections.shuffle(toReturn);
		return toReturn;
	}

	private static LUDOPlayer performTwoPointCrossover(LUDOPlayer player, LUDOPlayer player2)
	{
		int[] weights1 = ((DbjLUDOPlayer) player).getWeights();
		int[] weights2 = ((DbjLUDOPlayer) player2).getWeights();
		int point1, point2;
		do
		{
			point1 = (int) (Math.random() * weights1.length);
			point2 = (int) (Math.random() * weights1.length);
		} while (point1 >= point2);
		
		int[] newW = new int[weights1.length];
		
		for (int i = 0; i < point1; i++)
			newW[i] = weights1[i];
		for (int i = point1; i < point2; i++)
			newW[i] = weights2[i];
		for (int i = point2; i < weights1.length; i++)
			newW[i] = weights1[i];
		
		return new DbjLUDOPlayer(LUDO_new.board, newW);
	}

	private static int[] mutateWeights(int[] weights)
	{
		int randomNumber, probability = 50;
		for (int i = 0; i < weights.length; i++)
		{
			randomNumber = (int) (Math.random() * 1000);
			if (randomNumber < probability)
			{
				int tweak = (int)(Math.random() * 10) + 1;
				int desired = (int)(Math.random() * 10);
				weights[i] = desired<5 ? weights[i] + tweak : weights[i] - tweak;
				if (weights[i] > 100)
					weights[i] = 100;
				if (weights[i] < 0)
					weights[i] = 0;
			}
		}
		return weights;
	}
}
;